package com.apchernykh.rozhlasapp.utils;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class PreferencesCache {

	private static final String PREFERENCE_NAME_PODCAST_DOWNLOADER = "podcast_downloader";
	private static final String PREFERENCE_NAME_ONLY_WIFI = "only_wifi";
	private static final String PREFERENCE_NAME_LAST_STATION_URL = "last_station_url";
	private static final String PREFERENCE_NAME_LAST_STATION_NAME = "last_station_name";
	private static final String PREFERENCE_NAME_DESIRED_BITRATE = "bitrate_limitation";
	private static final boolean DEFAULT_ON_WIFI_ONLY = false;
	private static final String DEFAULT_LAST_STATION_URL = "";
	private static final String DEFAULT_LAST_STATION_NAME = "";
	private static final String DEFAULT_DESIRED_BITRATE = "high";
	private static final String DEFAULT_DOWNLOADER = "SilentRecorder";
	public String mDesiredBitrate = DEFAULT_DESIRED_BITRATE;
	public String mLastStationName = DEFAULT_LAST_STATION_NAME;
	public String mLastStationURL = DEFAULT_LAST_STATION_URL;
	public boolean mOnWiFiOnly = DEFAULT_ON_WIFI_ONLY;
	public String mPodcastRecorderName =  DEFAULT_DOWNLOADER;

	public void refresh(Context iContext) {
		SharedPreferences config = PreferenceManager
				.getDefaultSharedPreferences(iContext);
		mDesiredBitrate = config.getString(PREFERENCE_NAME_DESIRED_BITRATE,
				DEFAULT_DESIRED_BITRATE);
		mLastStationName = config.getString(PREFERENCE_NAME_LAST_STATION_NAME,
				DEFAULT_LAST_STATION_NAME);
		mLastStationURL = config.getString(PREFERENCE_NAME_LAST_STATION_URL,
				DEFAULT_LAST_STATION_URL);
		mOnWiFiOnly = config.getBoolean(PREFERENCE_NAME_ONLY_WIFI,
				DEFAULT_ON_WIFI_ONLY);
		mPodcastRecorderName = config.getString(PREFERENCE_NAME_PODCAST_DOWNLOADER,
				DEFAULT_DOWNLOADER);
	}

	public void save(Context iContext) {
		ConditionalLog.e(mLastStationName + ":" + mLastStationURL);
		SharedPreferences aConfig = PreferenceManager
				.getDefaultSharedPreferences(iContext);
		Editor aEditor = aConfig.edit();
		aEditor.putString(PREFERENCE_NAME_LAST_STATION_NAME, mLastStationName);
		aEditor.putString(PREFERENCE_NAME_LAST_STATION_URL, mLastStationURL);
		// we don't want to modify those preferences which are set up by
		// preferences activity
		// aEditor.putString(PREFERENCE_NAME_DESIRED_BITRATE, mDesiredBitrate);
		// aEditor.putBoolean(PREFERENCE_NAME_ONLY_WIFI, mOnWiFiOnly);
		aEditor.commit();
	}
}